package com.raising.sc.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class HelloService2 {

    @Autowired
    RestTemplate restTemplate;

    @HystrixCollapser(
            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
            batchMethod = "hiService2",
            collapserProperties = {
                    @HystrixProperty(name = "timerDelayInMilliseconds",value = "100")
            }
    )
    public String hiService1(String name) {
        log.info("进入 hiService1 ");
        return restTemplate.getForObject("http://service-C/hello?name="+name,String.class);
    }

    /**
     * 合并操作线程
     * @author GaoYuan
     * @date 2018/4/3 下午1:37
     */
    //参考 https://segmentfault.com/a/1190000012256788
    @HystrixCommand(commandProperties = @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds" , value = "5000"))
    public List<String> hiService2(List<String> name) {
        log.info("进入 hiService2 ");
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        log.info("合并操作线程 --> {} --> params --> {}", Thread.currentThread().getName(), name);
        return list;
    }

}